//========================================================================
// *作者：海盗猫  主页：ilplay.com
// *脚本：DrawDeployAssembly
// *描述：
// *?2021 MYJL . All rights reserved.
//* ======================================================================
#if UNITY_EDITOR
using UnityEngine;
using UnityEditor;
using HybridCLR.Editor.Commands;
using YooAsset.Editor;
using System.IO;

namespace HDMCode.Hotfix
{
    [UnityEditor.InitializeOnLoad]
    public class DrawDeployAssembly: DrawDeployAssemblyBase
    {
        private static GUIContent newAssembly;
        private static GUIContent updateAssembly;
        private static GUIContent openExETool;
        private static GUIContent newAssets;
        private static GUIContent updateAssets;
        private static GUIContent clearCache;

        static DrawDeployAssembly()
        {
            newAssembly = EditorGUIUtility.TrTextContentWithIcon(" 部署程序集", "构建全新热更新程序集", "sv_icon_dot6_sml");
            newAssets = EditorGUIUtility.TrTextContentWithIcon(" 部署热更新", "构建全新热更新程序集", "sv_icon_dot6_sml");
            updateAssembly = EditorGUIUtility.TrTextContentWithIcon(" 更新程序集", "更新当前项目的程序集", "sv_icon_dot2_sml");
            updateAssets = EditorGUIUtility.TrTextContentWithIcon(" 更新资源包", "更新当前项目的程序集", "sv_icon_dot2_sml");
            openExETool = EditorGUIUtility.TrTextContentWithIcon(" 预览", "用于预览实际效果的工具", "SceneViewRGB");
            clearCache = EditorGUIUtility.TrTextContentWithIcon(" 清理", "用于预览实际效果的工具", "Refresh");
            UnityEditorToolbar.RightToolbarGUI.Add(OnRightToolbarGUI);
            UnityEditorToolbar.LeftToolbarGUI.Add(OnLeftToolbarGUI);
        }

        private static void OnLeftToolbarGUI()
        {
            GUILayout.Box("",GUIStyle.none, GUILayout.ExpandWidth(true));
            if (GUILayout.Button(clearCache, EditorStyles.foldoutHeader, GUILayout.Width(70)))
            {
                if (Directory.Exists($"{System.Environment.CurrentDirectory}/yoo")) Directory.Delete($"{System.Environment.CurrentDirectory}/yoo", true);
            }
            GUILayout.Space(5);
            if (GUILayout.Button(updateAssembly, EditorStyles.foldoutHeader, GUILayout.ExpandWidth(true),GUILayout.MaxWidth(100)))
            {
                CompileDllCommand.CompileDllActiveBuildTarget();
                GetFiles();
            }
            GUILayout.Space(5);
            if (GUILayout.Button(updateAssets, EditorStyles.foldoutHeader, GUILayout.ExpandWidth(true), GUILayout.MaxWidth(100)))
            {
                DirectoryCheck();
                ToolBuildAssetsPackage buildAssets = new ToolBuildAssetsPackage(DrawMeun.drawUISetting.AssetsPackageName, EBuildMode.IncrementalBuild, false);
                ToolBuildScriptPackage buildScript = new ToolBuildScriptPackage(DrawMeun.drawUISetting.ScriptPackageName, EBuildMode.ForceRebuild, false);
                buildAssets.BuildAssetsPackge(() =>
                {
                    buildScript.BuildScriptPackge(() =>
                    {
                        EditorUtility.RevealInFinder(DrawMeun.drawUISetting.AssetsOutPutPath + "/");
                    });
                });
            }
        }
        private static void OnRightToolbarGUI()
        {
            if (GUILayout.Button(newAssembly, EditorStyles.foldoutHeader, GUILayout.Width(100)))
            {
                if (EditorUtility.DisplayDialog("程序集编译提示", "是否构建全新热更新程序集，构建全新包会更新引用信息和Link.xml，是否确认重新构建！", "确认", "取消"))
                {
                    PrebuildCommand.GenerateAll();
                    GetFiles();
                }
            }
            GUILayout.Space(5);
            if (GUILayout.Button(newAssets, EditorStyles.foldoutHeader, GUILayout.Width(100)))
            {
                string yooPath = Application.streamingAssetsPath + "/yoo";
                if (Directory.Exists(yooPath))
                    Directory.Delete(yooPath,true);
                PrebuildCommand.GenerateAll();
                DirectoryCheck();
                ToolBuildAssetsPackage buildAssets = new ToolBuildAssetsPackage(DrawMeun.drawUISetting.AssetsPackageName, EBuildMode.ForceRebuild, true);
                ToolBuildScriptPackage buildScript = new ToolBuildScriptPackage(DrawMeun.drawUISetting.ScriptPackageName, EBuildMode.ForceRebuild, true);
                buildAssets.BuildAssetsPackge(() =>
                {
                    buildScript.BuildScriptPackge(() =>
                    {
                        EditorUtility.RevealInFinder(DrawMeun.drawUISetting.AssetsOutPutPath + "/");
                    });
                });
            }
            GUILayout.Space(5);
            if (GUILayout.Button(openExETool, EditorStyles.foldoutHeader, GUILayout.Width(70)))
            {
                OpenEXETool("", DrawMeun.ToolAssetsPath);
            }
            GUILayout.Box("", GUIStyle.none, GUILayout.ExpandWidth(true));
        }
    }
}

#endif